﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text.RegularExpressions;
using HZ.Supervise.Model.Enum;
using System.Text;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;

namespace HZ.Supervise.Web.Secondphase.NewReport
{
    public partial class GoodsOrderDetail_DZ : BasePage
    {
        public GoodsOrderDetail_DZ()
        {
            this.ModuleNo = Module.实时报表.ToString();
            this.PageOperateType = OperateType.明细统计.ToString();
        }
        HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
        IBLL_User iu = BLLFactory.BLL_User();
        KeyOperationLogJG kj;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //BindData();
                GetAreaCount();
            }
        }

        /// <summary>
        /// 查询条件
        /// </summary>
        /// <returns></returns>
        string strwhere() { 
            StringBuilder strWhere = new StringBuilder ();
            #region 查询条件
            string str = "";
            //时间
            if (!string.IsNullOrEmpty(this.txtGoodsID.Text.Trim()))
            {
                str += string.Format(@"AND CHARINDEX('{0}',ProcureCatalogID)  >0", this.txtGoodsID.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtGoodsName.Text))
            {
                str += string.Format(@"AND CHARINDEX('{0}',GoodsName)  >0", this.txtGoodsName.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtOutLookc.Text))
            {
                str += string.Format(@"AND CHARINDEX('{0}',Outlookc)  >0", this.txtOutLookc.Text.Trim());
            } 
            if (!string.IsNullOrEmpty(this.txtCompanyName_SC.Text))
            {
                str += string.Format(@"AND CHARINDEX('{0}',companyName_TB)  >0", this.txtCompanyName_SC.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtCompanyName_PS.Text))
            {
                str += string.Format(@"AND CHARINDEX('{0}',CompanyName_PS)  >0", this.txtCompanyName_PS.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtOrderName.Text))
            {
                str += string.Format(@"AND CHARINDEX('{0}',OrderName)  >0", this.txtOrderName.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtTimeFrom.Text) )
            {
                str += " AND SubmiTime>='" + txtTimeFrom.Text.Trim() + "'";
            }
            if (!string.IsNullOrEmpty(this.txtTimeTo.Text))
            {
                str += " AND SubmiTime<'" + Convert.ToDateTime(txtTimeTo.Text.Trim()).AddDays(1) + "'";
            }
            #endregion

            return str.ToString();
        }

        /// <summary>
        /// 绑定数据源
        /// </summary>
        public void BindData()
        {
            int t = 0; 
            DataTable dt = ie.GoodsOrderDetail_DZ(strwhere(), 0, this.AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out t);
            AspNetPager1.RecordCount = t;
            gvwGoodsTotal.PageSize = AspNetPager1.PageSize;
            this.gvwGoodsTotal.DataSource = dt;
            this.gvwGoodsTotal.DataBind();
            AddLog(string.Format("查询实时明细低值统计数据!"));
        } 
        /// <summary>
        /// 分页绑定
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        /// <summary>
        /// 导出按钮
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void btnExcel1_Click(object sender, EventArgs e)
        {
            int t = 0;
            DataTable dt = ie.GoodsOrderDetail_DZ(strwhere(), 0, 1, 0, out t);
            AddLog(string.Format("导出实时明细低值统计数据成功!"));
            ExportFile.Instance.ExportData(dt, gvwGoodsTotal, this.Page);
        }

        /// <summary>
        /// 时间查询条件
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
        }

        /// <summary>
        /// 汇总
        /// </summary>
        public void GetAreaCount()
        {
            decimal PurchaseAmount = 0;//采购总金额
            decimal DistributeAmount = 0;//配送金额
            decimal WarehouseAmount = 0;//入库金额
            int s = 0;
            int totalCount = 0;
            DataTable dt = ie.GoodsOrderDetail_DZ(strwhere(), 1, 0, 0, out totalCount);
            if (dt.Rows.Count > 0)
                foreach (DataRow dr in dt.Rows)
                {
                    PurchaseAmount += Convert.ToDecimal(dr["PurchaseOrderAmount"]);
                    DistributeAmount += Convert.ToDecimal(dr["DistributeOrderAmount"]);
                    WarehouseAmount += Convert.ToDecimal(dr["WarehouseOrederAmount"]);
                    s++;
                }
            if (gvwGoodsTotal.Rows.Count > 0)
            {
                lblCount.Text = "采购总金额为" + PurchaseAmount.ToString("F2") + "元，配送总金额为" + DistributeAmount.ToString("F2") + "元，入库总金额为" + WarehouseAmount.ToString("F2") + "元";
            }
            else
            {
                lblCount.Text = "暂无数据！";
            }

        }
        /// <summary>
        /// 添加日志
        /// </summary>
        /// <param name="msg"></param>
        private void AddLog(string msg)
        {
            kj = new KeyOperationLogJG();
            kj.OperationLogSerialID = Guid.NewGuid().ToString();
            kj.Operator = CurrentAccount.GetUser().UserID;
            kj.OrgID = CurrentAccount.GetUser().Org.OrgID;
            kj.OperateTime = DateTime.Now;
            kj.OperateMessage = msg;
            iu.AddKeyOperationLog(kj);
        }
    }
}